Iteration Examples

Print first N natural numbers

Step 1 : Start
Step 2 : Initialize the value of i as 1
Step 3: Read the value of a number, N
Step 4 : Check the condition i less than or equal to N, if the condition is true goto step 4.1 else goto step 5
Step 4.1: Print i and increment the value of i by 1
Step 4.2: Repeat the Step 4 until the condition is true
Step 5 : Stop

Print Sum of first N natural numbers

Step 1 : Start
Step 2 : Initialize the value of i as 1 and sum as 0
Step 3: Read the value of a number, N
Step 4 : Check the condition i less than or equal to N, if the condition is true goto step 4.1 else goto step 5
Step 4.1 calculate sum=sum+i
Step 4.2: Increment the value of i by 1 and repeat the Step 4 until the condition is true
Step 5 : print sum
Step 6 : Stop

Print odd numbers from 1 to N

Step 1: Start
Step 2: Initialize the value of i as 1
Step 3: Read the value of a number, N
Step 4: Check the condition i less than or equal to N, if the condition is true goto step 4.1 else
goto step 5
Step 4.1: print i and increment the value of i by 2
Step 4.2: Repeat the Step 4 until the condition is true
Step 5: Stop

Print multiplication table of a given number

Step 1: Start
Step 2: Initialize the value of i as 1
Step 3: Read the value of a number, N
Step 4: Check the condition i less than or equal to 10, if the condition is true goto step 4.1 else
goto step 5
Step 4.1: Calculate res=N*i
Step 4.1: print N*i=res and increment the value of i by 1
Step 4.2: Repeat the Step 4 until the condition is true
Step 5: Stop

Print reverses of the given number

Step 1: Start
Step 2: Initialize the value of reverse as 0 and reminder as 0
Step 3: Read any number, N
Step 4: Check the condition N is not equal to zero, if the condition is true goto step 4.1 else
goto step 5
Step 4.1: Calculate reminder = N %10
Step 4.2: Calculate reverse = reverser*10+ reminder
Step 4.3: Calculate N=N/10 and repeat the Step 4 until the condition is true
Step 5: print reverse
Step 6: Stop

To find factorial of a given number

Step 1: Start
Step 2: Initialize the value of i as 1 and fact as 1
Step 3: Read the value of a number, num
Step 4: Check the condition num equal to 0, if the condition is true goto step 4.1 else goto step 4.2
Step 4.1: print “Factorial of 0 is 1
Step 4.2: Check the condition i less than or equal to num, if the condition is true goto
step 4.2.1 else goto step 4.3
Step 4.2.1: Calculate factorial as, fact=fact*i and increment the value of i by 1
Step 4.2.2: Repeat the Step 4.2 until the condition is true
Step 4.3: print fact
Step 5: Stop

References

  • Allen B. Downey, “Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated for Python 3, Shroff/O‘Reilly Publishers, 2016 (http://greenteapress.com/wp/thinkpython/)
  • Guido van Rossum and Fred L. Drake Jr, ―An Introduction to Python – Revised and updated for Python 3.2, Network Theory Ltd., 2011.
  • John V Guttag, ―Introduction to Computation and Programming Using Python‘‘, Revised and expanded Edition, MIT Press , 2013
  • Robert Sedgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
  • Timothy A. Budd, ―Exploring Python‖, Mc-Graw Hill Education (India) Private Ltd.,, 2015. 4. Kenneth A. Lambert, ―Fundamentals of Python: First Programs‖, CENGAGE Learning, 2012.
  • Charles Dierbach, ―Introduction to Computer Science using Python: A Computational Problem-Solving Focus, Wiley India Edition, 2013.
  • Paul Gries, Jennifer Campbell and Jason Montojo, ―Practical Programming: An Introduction to Computer Science using Python 3‖, Second edition, Pragmatic Programmers, LLC, 2013.